import { apiClient, apiRoutes } from "../../../utils/api"

// pages/group/group.js
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    id: app.globalData.orgid,
    name: app.globalData.orgname,
    users: [],
    lists: [
      { 'listname':'传感器','tablename': 'devices', 'items': [] },
      { 'listname':'开关','tablename': 'switches', 'items': [] },
      { 'listname':'摄像头','tablename': 'cameras', 'items': [] }
    ],
    activeTab: 0,
    scrollLeft: 0
  },
  nav_to(e) {
    //保存这个组织的用户和设备
    // app.globalData.users = this.data.users
    app.globalData.lists = this.data.lists
    // 获取所点击内容的ID
    let id = e.currentTarget.dataset.id ? e.currentTarget.dataset.id : 0
    // 确定锁点击内容的类别
    let url = e.currentTarget.dataset.tablename
    const match = { "users": "user", 'devices': 'device', 'switches': 'switch', 'cameras': 'camera' }
    console.log(this.data)
    // 保存这个用户信息
    if (this.data.users) {
      app.globalData.user = this.data.users.find(user => user.id == id)
    }
    // 还要保存该组织的信息，但在onload里面已保存过
    // console.log(user)
    console.log("id", id)
    console.log("urlname", url)
    console.log("url", `/pages/data/${match[url]}/${match[url]}?id=${id}`)
    wx.navigateTo({
      url: `/pages/data/${match[url]}/${match[url]}?id=${id}`,
    })
  },
  switchTab: function (e) {
    const index = e.currentTarget.dataset.index;
    this.setData({
      activeTab: index,
      scrollLeft: index * 750 // 根据屏幕宽度调整
    });
  },
  delete(e) {
    const tablename = e.currentTarget.dataset.tablename
    const id = e.currentTarget.dataset.id
    const url = apiRoutes.base + tablename + '/' + id + '/'
    console.log(url)
    wx.showModal({
      title: '删除确认',
      content: '确认删除吗？',
      complete: (res) => {
        if (res.confirm) {
          apiClient.delete(url).then(res => {
            // console.log(res)
            if (res.statusCode == 204) {
              wx.showToast({
                title: '删除成功',
              })
              this.init_data()
            } else {
              wx.showToast({
                title: '删除失败',
                icon: 'error'
              })
            }
          })
        }
      }
    })
  },
  async init_data() {
    apiClient.get(apiRoutes.group_properties + app.globalData.orgid + "/").then(res => {
      // console.log(res.data)
      if (res.data) {
        this.setData({
          users: res.data.users,
          lists: [
            { 'listname': '传感器', 'tablename': 'devices', 'items': res.data.devices },
            { 'listname': '开关', 'tablename': 'switches', 'items': res.data.switches },
            { 'listname': '摄像头', 'tablename': 'cameras', 'items': res.data.cameras }
          ],
        })
      }

      // console.log("in group.js",this.data.users, this.data.lists)
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setData({ id: app.globalData.orgid, name: app.globalData.orgname })
    this.init_data()
    // console.log(this.data.id)
  },
  onShow(){
    this.init_data()
  }
})